package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.LiveItem;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.beans.SearchDataBean;
import com.sctvcloud.dongqu.beans.TopicItem;
import com.sctvcloud.dongqu.ui.util.IListShowData;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.utils.SkipUtil;
import com.sctvcloud.dongqu.ui.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SubTopicNewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<NewsItem> dataList = new ArrayList<>();
    private Context ctx;

    public SubTopicNewsListAdapter(Context ctx, List<NewsItem> dataList ) {
        this.ctx = ctx;
        this.dataList = dataList;
    }

    static class TopicNewsViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout;
        CustomFontTextView newsTitle;
        CustomFontTextView newsTime;
        CustomFontTextView clickcount;

        public TopicNewsViewHolder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.news_item_layout);
            newsTitle = (CustomFontTextView) view.findViewById(R.id.news_title);
            newsTime = (CustomFontTextView) view.findViewById(R.id.news_time);
            clickcount = (CustomFontTextView) view.findViewById(R.id.newes_detail_click_count);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.topic_news_item, parent, false);
        holder = new TopicNewsViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (null != dataList && dataList.size() > position) {
            final NewsItem data = dataList.get(position);
            TopicNewsViewHolder h = (TopicNewsViewHolder) holder;
            h.newsTitle.setText(data.getNewsTitle());
            h.clickcount.setText(UIUtils.getReadingNumber(data.getReadCount()));
            h.newsTime.setText(DateUtils.formatDate(DateUtils.getDataDate(data), ""));
            h.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SkipUtil.skipWithNewsItem(ctx, data, new HashMap<String, String>());
                }
            });
        }
    }

    public void setDataList(List<NewsItem> list) {
        this.dataList = list;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        if (null != dataList) {
            return dataList.size();
        } else {
            return 0;
        }
    }

}
